/*
 * @Author: t-xiangqun.li t-xiangqun.li@pcitc.com
 * @Date: 2025-10-22 14:20:54
 * @LastEditors: t-xiangqun.li t-xiangqun.li@pcitc.com
 * @LastEditTime: 2025-10-22 14:23:04
 * @FilePath: \electrconVue\src\utils\demo.ts
 * @Description: 学习百度openAI 和 阿里云百炼大模型
 */

import OpenAI from "openai";
import "dotenv/config"; // dotenv 模块允许我们将所有的环境变量存储在一个名为 .env 的文件中，并在应用启动时加载这些变量。
// import fs from "node:fs/promises"; // 读图
import fs from "fs"; // 读文件

const client = new OpenAI({
  apiKey: process.env.ALI_API_KEY,
  baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1", // "https://qianfan.baidubce.com/v2/", // 千帆ModelBuilder平台地址
});

// const imageBuffer = await fs.readFile("../dog.jpg");
// const base64Image = imageBuffer.toString("base64");
// console.log(base64Image, "base64");
async function main() {
  // 识别图片
  // const ret = await client.chat.completions.create({
  //   messages: [
  //     {
  //       role: "user",
  //       content: [
  //         { type: "text", text: "图中是什么动物？" },
  //         {
  //           type: "image_url",
  //           image_url: { url: `data:image/jpg;base64,${base64Image}` },
  //         },
  //       ],
  //     },
  //   ],
  //   model: "qwen-vl-plus",
  // });
  // console.log(ret.choices[0].message, "123123123");

  // 识别文件内容
  const fileObject = await client.files.create({
    file: fs.createReadStream("../test.txt"),
    purpose: "file-extract" as any,
  });
  const ret = await client.chat.completions.create({
    model: "qwen-long",
    messages: [
      { role: "system", content: `fileid://${fileObject.id}` },
      { role: "user", content: "这篇文档问了什么？" },
    ],
  });
  console.log(JSON.stringify(ret));
}
// async function main() {
//   const stream = await client.chat.completions.create({
//     messages: [{ role: "user", content: "故宫在哪里" }],
//     model: "ernie-tiny-8k", //模型对应的model值，请查看支持的模型列表：https://cloud.baidu.com/doc/qianfan-docs/s/7m95lyy43
//     stream: true,
//   });
//   for await (const chunk of stream) {
//     console.log(chunk.choices[0].delta, "123123123");
//   }
// }

main();
